package com.zhaoqi.hr.controller;

import com.zhaoqi.hr.dto.request.JobApplicationRequest;
import com.zhaoqi.hr.dto.request.JobPostingRequest;
import com.zhaoqi.hr.dto.response.ApiResponse;
import com.zhaoqi.hr.dto.response.JobApplicationResponse;
import com.zhaoqi.hr.dto.response.JobPostingResponse;
import com.zhaoqi.hr.entity.JobApplication;
import com.zhaoqi.hr.entity.JobPosting;
import com.zhaoqi.hr.security.CurrentUser;
import com.zhaoqi.hr.security.UserPrincipal;
import com.zhaoqi.hr.service.JobService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 岗位管理控制器
 */
@RestController
@RequestMapping("/jobs")
public class JobController {

    private static final Logger log = LoggerFactory.getLogger(JobController.class);

    private final JobService jobService;

    public JobController(JobService jobService) {
        this.jobService = jobService;
    }

    /**
     * 发布岗位（雇主专用）
     */
    @PostMapping("/publish")
    public ApiResponse<JobPostingResponse> publishJob(
            @CurrentUser UserPrincipal currentUser,
            @Valid @RequestBody JobPostingRequest request) {
        
        JobPostingResponse response = jobService.publishJob(currentUser.getId(), request);
        return ApiResponse.success(response);
    }

    /**
     * 获取岗位列表（分页）
     */
    @GetMapping
    public ApiResponse<Page<JobPostingResponse>> getJobPostings(
            Pageable pageable,
            @RequestParam(required = false) JobPosting.JobStatus status,
            @RequestParam(required = false) JobPosting.JobType jobType,
            @RequestParam(required = false) String location) {
        
        Page<JobPostingResponse> jobs = jobService.getJobPostings(pageable, status, jobType, location);
        return ApiResponse.success(jobs);
    }

    /**
     * 获取岗位详情
     */
    @GetMapping("/{jobId}")
    public ApiResponse<JobPostingResponse> getJobPosting(@PathVariable Long jobId) {
        JobPostingResponse job = jobService.getJobPostingById(jobId);
        return ApiResponse.success(job);
    }

    /**
     * 获取我发布的岗位列表（雇主专用）
     */
    @GetMapping("/my")
    public ApiResponse<List<JobPostingResponse>> getMyJobPostings(
            @CurrentUser UserPrincipal currentUser) {
        
        List<JobPostingResponse> jobs = jobService.getEmployerJobPostings(currentUser.getId());
        return ApiResponse.success(jobs);
    }

    /**
     * 更新岗位状态（雇主专用）
     */
    @PutMapping("/{jobId}/status")
    public ApiResponse<JobPostingResponse> updateJobStatus(
            @CurrentUser UserPrincipal currentUser,
            @PathVariable Long jobId,
            @RequestParam JobPosting.JobStatus status) {
        
        JobPostingResponse response = jobService.updateJobStatus(currentUser.getId(), jobId, status);
        return ApiResponse.success(response);
    }

    /**
     * 申请岗位（劳动者专用）
     */
    @PostMapping("/apply")
    public ApiResponse<JobApplicationResponse> applyForJob(
            @CurrentUser UserPrincipal currentUser,
            @Valid @RequestBody JobApplicationRequest request) {
        
        JobApplicationResponse response = jobService.applyForJob(currentUser.getId(), request);
        return ApiResponse.success(response);
    }

    /**
     * 获取岗位申请列表（雇主专用）
     */
    @GetMapping("/{jobId}/applications")
    public ApiResponse<List<JobApplicationResponse>> getJobApplications(
            @CurrentUser UserPrincipal currentUser,
            @PathVariable Long jobId) {
        
        List<JobApplicationResponse> applications = jobService.getJobApplications(
                currentUser.getId(), jobId);
        return ApiResponse.success(applications);
    }

    /**
     * 获取我的申请列表（劳动者专用）
     */
    @GetMapping("/applications/my")
    public ApiResponse<List<JobApplicationResponse>> getMyApplications(
            @CurrentUser UserPrincipal currentUser) {
        
        List<JobApplicationResponse> applications = jobService.getWorkerApplications(currentUser.getId());
        return ApiResponse.success(applications);
    }

    /**
     * 处理岗位申请（雇主专用）
     */
    @PutMapping("/applications/{applicationId}")
    public ApiResponse<JobApplicationResponse> processApplication(
            @CurrentUser UserPrincipal currentUser,
            @PathVariable Long applicationId,
            @RequestParam JobApplication.ApplicationStatus status,
            @RequestParam(required = false) String reply) {
        
        JobApplicationResponse response = jobService.processApplication(
                currentUser.getId(), applicationId, status, reply);
        return ApiResponse.success(response);
    }
}
